package com.mock.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.mock.dao.DepartmentDao;
import com.mock.model.DataTable;
import com.mock.model.Department;
import com.mock.model.Team;
import com.mock.utils.HibernateUtil;

public class DepartmentImpl extends GenericDaoImpl<Department, Long> implements DepartmentDao{

	@Override
	public int getRowCount() {
		return super.getRowCount(Department.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getDepartmentByDirectorateId(DataTable dt, long directorateId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Department.class, "d");
		
		criteria.createAlias("d.leadContact", "contact");
		criteria.setFetchMode("d.leadContact", FetchMode.JOIN);
		criteria.createAlias("d.directorate", "directorate");
		criteria.setFetchMode("d.directorate", FetchMode.JOIN);
		
		
		
		//Get Department by DirectorateId
		criteria.add(Restrictions.like("directorate.directorateId", directorateId));
		
		// Filter by Letter
		Disjunction disjunction = Restrictions.disjunction();
		if ("^[0-9]".equalsIgnoreCase(dt.sSearch_0)) {
			for (int i = 0; i < 10; i++) {
				disjunction.add(Restrictions.ilike("d.departmentName", i + "%"));
			}
			criteria.add(disjunction);
		} else {
			criteria.add(Restrictions.ilike("d.departmentName", dt.sSearch_0.replace("^", "") + "%"));
		}
		
		// Get Sort Column Name
		String columnName = "name";
		switch (dt.iSortCol_0) {
		case 0:
			columnName = "departmentName";
			break;
		case 1:
			columnName = "address1";
			break;
		case 2:
			columnName = "postcode";
			break;
		case 3:
			if ("asc".equalsIgnoreCase(dt.sSortDir_0)) {
				criteria.addOrder(Order.asc("contact.firstname"));
				criteria.addOrder(Order.asc("contact.surname"));
			} else {
				criteria.addOrder(Order.desc("contact.firstname"));
				criteria.addOrder(Order.desc("contact.surname"));
			}
			columnName = "contact.firstname";
			break;
		case 4:
			columnName = "active";
			break;
		default:
			break;
		}

		// Get Sort Direction
		if ("asc".equalsIgnoreCase(dt.sSortDir_0) && dt.iSortCol_0 != 3) {
			criteria.addOrder(Order.asc(columnName));
		} else {
			criteria.addOrder(Order.desc(columnName));
		}
		
		// Filter in-active
		if ("".equalsIgnoreCase(dt.sSearch_4)) {

		} else if ("Yes".equalsIgnoreCase(dt.sSearch_4)) {
			criteria.add(Restrictions.eq("active", true));
		}

		// Filter
		criteria.setFirstResult(dt.iDisplayStart);
		criteria.setMaxResults(dt.iDisplayLength);
		
		
		List<Department> departments = criteria.list();
		session.getTransaction().commit();
		return departments;
	}

}
